"""
配置文件，包含所有可配置参数。
Configuration file containing all configurable parameters.
"""
import tempfile
import os
from typing import List

# Elasticsearch 配置
ELASTICSEARCH_HOSTS = os.getenv('ELASTICSEARCH_HOSTS', 'http://119.23.229.39:39200').split(',')
ELASTICSEARCH_REQUEST_TIMEOUT = int(os.getenv('ELASTICSEARCH_REQUEST_TIMEOUT', '10'))
ELASTICSEARCH_USERNAME = os.getenv('ELASTICSEARCH_USERNAME', 'elastic')
ELASTICSEARCH_PASSWORD = os.getenv('ELASTICSEARCH_PASSWORD', 'zl800800')

# Redis 配置
REDIS_HOST = os.getenv('REDIS_HOST', '120.79.123.134')
REDIS_PORT = int(os.getenv('REDIS_PORT', '26000'))
REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', 'jingzhongyue1')
REDIS_MAX_CONNECTIONS = int(os.getenv('REDIS_MAX_CONNECTIONS', '100'))
REDIS_SOCKET_TIMEOUT = int(os.getenv('REDIS_SOCKET_TIMEOUT', '10'))

# FastAPI 配置
FASTAPI_HOST = "0.0.0.0"
FASTAPI_PORT = 8991

# 日志配置
LOG_LEVEL = "INFO"
LOG_FILE = "handband_collector.log"

# 业务配置
BATCH_SIZE = 100  # 批量存储大小
MAX_RETRIES = 3  # 最大重试次数
RETRY_DELAY = 1  # 重试延迟（秒）

# 时间timestamp 基准值
BASE_TIMESTAMP = 1735660800  # 基础timestamp 值,目的是缩小比对时间所需要的内存值

# 旷工阈值(秒) / Absenteeism threshold (seconds)
NOT_WORK_MILLION = 7200

# IoT模块配置 / IoT Module Configuration
IOT_CONFIG = {
    "device_monitoring": {
        "offline_threshold_minutes": int(os.getenv('IOT_OFFLINE_THRESHOLD', '10')),  # 设备离线阈值(分钟) / Device offline threshold (minutes)
        "heartbeat_timeout_minutes": int(os.getenv('IOT_HEARTBEAT_TIMEOUT', '5')),  # 心跳超时阈值(分钟) / Heartbeat timeout threshold (minutes)
        "health_check_interval": int(os.getenv('IOT_HEALTH_CHECK_INTERVAL', '60')),  # 健康检查间隔(秒) / Health check interval (seconds)
        "max_concurrent_processing": int(os.getenv('IOT_MAX_CONCURRENT', '50')),  # 最大并发处理数 / Maximum concurrent processing
        "cleanup_threshold_hours": int(os.getenv('IOT_CLEANUP_THRESHOLD', '24')),  # 设备清理阈值(小时) / Device cleanup threshold (hours)
        "max_devices": int(os.getenv('IOT_MAX_DEVICES', '10000'))  # 最大设备数量 / Maximum device count
    },
    "data_processing": {
        "processing_timeout": int(os.getenv('IOT_PROCESSING_TIMEOUT', '30')),  # 数据处理超时(秒) / Data processing timeout (seconds)
        "location_timeout": int(os.getenv('IOT_LOCATION_TIMEOUT', '30')),  # 定位处理超时(秒) / Location processing timeout (seconds)
        "health_timeout": int(os.getenv('IOT_HEALTH_TIMEOUT', '45')),  # 健康数据处理超时(秒) / Health data processing timeout (seconds)
        "audio_timeout": int(os.getenv('IOT_AUDIO_TIMEOUT', '60')),  # 音频处理超时(秒) / Audio processing timeout (seconds)
        "batch_size": int(os.getenv('IOT_BATCH_SIZE', '100')),  # 批处理大小 / Batch processing size
        "max_batch_size": int(os.getenv('IOT_MAX_BATCH_SIZE', '1000')),  # 最大批处理大小 / Maximum batch processing size
        "db_operation_timeout": int(os.getenv('IOT_DB_TIMEOUT', '10')),  # 数据库操作超时(秒) / Database operation timeout (seconds)
        "enable_preprocessing": os.getenv('IOT_ENABLE_PREPROCESSING', 'true').lower() == 'true'  # 启用预处理 / Enable preprocessing
    },
    "command_handling": {
        "default_timeout": int(os.getenv('IOT_COMMAND_TIMEOUT', '30')),  # 默认命令超时(秒) / Default command timeout (seconds)
        "max_queue_size": int(os.getenv('IOT_MAX_QUEUE_SIZE', '5000')),  # 最大队列大小 / Maximum queue size
        "max_history_size": int(os.getenv('IOT_MAX_HISTORY', '1000')),  # 最大历史记录数 / Maximum history size
        "batch_command_concurrency": int(os.getenv('IOT_BATCH_CONCURRENCY', '50')),  # 批量命令并发数 / Batch command concurrency
        "single_device_concurrency": int(os.getenv('IOT_SINGLE_CONCURRENCY', '10')),  # 单设备命令并发数 / Single device command concurrency
        "enable_retry": os.getenv('IOT_ENABLE_RETRY', 'true').lower() == 'true'  # 启用重试 / Enable retry
    },
    "error_handling": {
        "max_retries": int(os.getenv('IOT_MAX_RETRIES', '3')),  # 最大重试次数 / Maximum retry count
        "retry_delay": float(os.getenv('IOT_RETRY_DELAY', '1.0')),  # 重试延迟(秒) / Retry delay (seconds)
        "circuit_breaker_threshold": int(os.getenv('IOT_CIRCUIT_THRESHOLD', '5')),  # 断路器阈值 / Circuit breaker threshold
        "circuit_breaker_timeout": int(os.getenv('IOT_CIRCUIT_TIMEOUT', '60'))  # 断路器超时(秒) / Circuit breaker timeout (seconds)
    },
    "validation": {
        "strict_validation": os.getenv('IOT_STRICT_VALIDATION', 'False').lower() == 'true',  # 严格验证模式 / Strict validation mode
        "data_confidence_threshold": float(os.getenv('IOT_CONFIDENCE_THRESHOLD', '0.7')),  # 数据置信度阈值 / Data confidence threshold
        "required_fields_check": os.getenv('IOT_REQUIRED_FIELDS_CHECK', 'True').lower() == 'true'  # 必填字段检查 / Required fields check
    },
    "logging": {
        "iot_log_level": os.getenv('IOT_LOG_LEVEL', 'INFO'),  # IoT模块日志级别 / IoT module log level
        "device_log_retention_days": int(os.getenv('IOT_LOG_RETENTION', '30')),  # 设备日志保留天数 / Device log retention days
        "memory_limit_mb": int(os.getenv('IOT_MEMORY_LIMIT', '512')),  # 内存使用限制(MB) / Memory usage limit (MB)
        "enable_metrics": os.getenv('IOT_ENABLE_METRICS', 'True').lower() == 'true'  # 启用性能指标 / Enable performance metrics
    }
}

CONFIG = {
    "email": {
        "smtp_server": os.getenv('SMTP_SERVER', 'smtp.qq.com'),
        "smtp_port": int(os.getenv('SMTP_PORT', '465')),
        "sender_email": os.getenv('SENDER_EMAIL', '65900370@qq.com'),
        "sender_password": os.getenv('SENDER_PASSWORD'),
        "use_ssl": os.getenv('USE_SSL', 'true').lower() == 'true'
    },
    "pdf": {
        "wkhtmltopdf_path": os.getenv('WKHTMLTOPDF_PATH', r"E:\\wkhtmltopdf\bin\wkhtmltopdf.exe"),
        "temp_dir": os.getenv('PDF_TEMP_DIR', tempfile.gettempdir())
    },
    "logging": {
        "log_file": os.getenv('LOG_FILE', 'report_generator.log'),
        "level": os.getenv('LOG_LEVEL', 'INFO')
    }
}
